/*    */ package com.qmsk.business.wechat.bean;
/*    */ 
/*    */ import com.qmsk.business.weixin.customMenu.weiXinUtil.WeiXinUtil;
/*    */ import java.io.IOException;
/*    */ import java.util.HashMap;
/*    */ import java.util.Map;
/*    */ import java.util.regex.Matcher;
/*    */ import java.util.regex.Pattern;
/*    */ import org.apache.log4j.Logger;
/*    */ 
/*    */ public class OAuthCache
/*    */ {
/* 15 */   private static Logger logger = Logger.getLogger(OAuthCache.class);
/*    */ 
/* 17 */   private static Map<String, String> cache = new HashMap();
/* 18 */   private static Map<String, String> ucache = new HashMap();
/*    */ 
/*    */   private static String analyzeOpenId(String json)
/*    */   {
/* 24 */     Pattern p = Pattern.compile("\"openid\":\"[^,]*\"");
/* 25 */     Matcher m = p.matcher(json);
/* 26 */     String key = null;
/* 27 */     if (m.find()) {
/* 28 */       key = m.group();
/*    */     }
/* 30 */     if (key == null)
/* 31 */       return null;
/* 32 */     return key.substring(10, key.length() - 1);
/*    */   }
/*    */ 
/*    */   public static synchronized String getOpenId(String code) throws IOException {
/* 36 */     logger.debug("getOpenId, code:" + code);
/*    */ 
/* 38 */     if (cache.containsKey(code)) {
/* 39 */       return ((String)cache.get(code));
/*    */     }
/*    */ 
/* 42 */     String json = WeiXinUtil.getAccess_token_Web(code);
/*    */ 
/* 44 */     if (json == null) {
/* 45 */       return "";
/*    */     }
/* 47 */     String openId = analyzeOpenId(json);
/* 48 */     if (openId == null) {
/* 49 */       return "";
/*    */     }
/*    */ 
/* 52 */     String codeTemp = (String)ucache.get(openId);
/* 53 */     if (codeTemp != null) {
/* 54 */       cache.remove(codeTemp);
/* 55 */       ucache.remove(openId);
/*    */     }
/*    */ 
/* 58 */     cache.put(code, openId);
/* 59 */     ucache.put(openId, code);
/* 60 */     logger.debug("cache size:" + cache.size() + " ucache size:" + 
/* 61 */       ucache.size());
/* 62 */     logger.debug("getOpenId, code:" + code + " OpenId:" + openId);
/* 63 */     return openId;
/*    */   }
/*    */ }

/* Location:           C:\Users\Administrator\Desktop\新建文件夹\WEB-INF\classes\
 * Qualified Name:     com.qmsk.business.wechat.bean.OAuthCache
 * JD-Core Version:    0.5.3
 */